const router = require('koa-router')()
const db = require('../db/initdb')
const path = require('path')
const fs = require('fs')

// 个人页面-作品列表
router.get("/person/works", async (ctx, next) => {
    console.log('works ' + ctx.query.userid)
    let userid = ctx.query.userid;

    // 作品id,作者昵称，作品作者，作品标题，作品详情，作品类型
    let sql = `select 
            hs.works.id,
            hs.user.name,
            hs.user.avatar,
            hs.works.detail,
            hs.works.picture,
            hs.works.tu
            from hs.works, hs.user
            where hs.works.userid = hs.user.id and audit = true and hs.works.userid=$1
            order by date desc, hs.works.id desc`;
    let result = await db.query(sql, [userid])
    // console.log(result.rows)
    let works = result.rows;

    // 图片地址 or 封面,视频地址
    sql = `select hs.picaddress.workid, address, perface
            from hs.picaddress, hs.works
            where hs.picaddress.workid = hs.works.id`;
    result = await db.query(sql)
    // console.log(result.rows)
    let address = result.rows;

    works.map(item => {
        let workid = item.id;
        // console.log(address.filter(item => item.workid == workid))
        if (item.picture) {
            // 图片类型
            let addressList = [];
            address.filter(item => item.workid == workid).map(item => {
                addressList.push(item.address)
            })
            item['addressList'] = addressList;
        } else {
            if (address.filter(item => item.workid == workid)[0]) {
                // 视频类型
                item['address'] = address.filter(item => item.workid == workid)[0].address;
                item['perface'] = address.filter(item => item.workid == workid)[0].perface;
            } else {
                // 文字类型
            }
        }
    })
    // console.log(works)

    ctx.body = works;
})

// 个人页面-点赞列表
router.get("/user/star/like", async (ctx, next) => {
    console.log('like ' + ctx.query.userid)
    let userid = ctx.query.userid;

    let sql = `select 
                hs.works.id,
                hs.user.name,
                hs.user.avatar,
                hs.works.detail,
                hs.works.picture
                from hs.star, hs.works, hs.user
                where hs.star.userid=$1 and hs.star.id = hs.works.id and hs.works.userid = hs.user.id and hs.star.likeo = true
                order by hs.star.likedate desc`;
    let result = await db.query(sql, [userid])
    // console.log(result.rows)
    let like = result.rows;

    // 图片地址 or 封面,视频地址
    sql = `select hs.picaddress.workid, address, perface
            from hs.picaddress, hs.works
            where hs.picaddress.workid = hs.works.id`;
    result = await db.query(sql)
    let address = result.rows;

    like.map(item => {
        let workid = item.id;
        // console.log(address.filter(item => item.workid == workid))
        if (item.picture) {
            // 图片类型
            let addressList = [];
            address.filter(item => item.workid == workid).map(item => {
                addressList.push(item.address)
            })
            item['addressList'] = addressList;
        } else {
            if (address.filter(item => item.workid == workid)[0]) {
                // 视频类型
                item['address'] = address.filter(item => item.workid == workid)[0].address;
                item['perface'] = address.filter(item => item.workid == workid)[0].perface;
            } else {
                // 文字类型
            }
        }
    })
    // console.log(like)

    ctx.body = like
})

// 个人页面-收藏列表
router.get("/user/star/collect", async (ctx, next) => {
    console.log('collect ' + ctx.query.userid)
    let userid = ctx.query.userid;

    let sql = `select 
                hs.works.id,
                hs.user.name,
                hs.user.avatar,
                hs.works.detail,
                hs.works.picture
                from hs.star, hs.works, hs.user
                where hs.star.userid=$1 and hs.star.id = hs.works.id and hs.works.userid = hs.user.id and hs.star.collect = true
                order by hs.star.collectdate desc`;
    let result = await db.query(sql, [userid])
    // console.log(result.rows)
    let collect = result.rows;

    // 图片地址 or 封面,视频地址
    sql = `select hs.picaddress.workid, address, perface
            from hs.picaddress, hs.works
            where hs.picaddress.workid = hs.works.id`;
    result = await db.query(sql)
    let address = result.rows;

    collect.map(item => {
        let workid = item.id;
        if (item.picture) {
            // 图片类型
            let addressList = [];
            address.filter(item => item.workid == workid).map(item => {
                addressList.push(item.address)
            })
            item['addressList'] = addressList;
        } else {
            if (address.filter(item => item.workid == workid)[0]) {
                // 视频类型
                item['address'] = address.filter(item => item.workid == workid)[0].address;
                item['perface'] = address.filter(item => item.workid == workid)[0].perface;
            } else {
                // 文字类型
            }
        }
    })
    // console.log(collect)

    ctx.body = collect
})

// 加载图片
router.get("/person/works/image/:name", async (ctx, next) => {
    // console.log(ctx.params.name)
    let filePath = path.join(__dirname, `../public/images/${ctx.params.name}`);
    let fileContent = fs.readFileSync(filePath)
    ctx.set("Content-Type", "image/jpg")
    ctx.body = fileContent;
})
router.get("/person/works/images/:name", async (ctx, next) => {
    console.log(ctx.params.name)
    sql = `select hs.picaddress.workid, address, perface
    from hs.picaddress, hs.works
    where hs.picaddress.workid = hs.works.id and hs.works.id=$1`;
    result = await db.query(sql, [ctx.params.name])
    console.log(result.rows)
    let filePath
    if (result.rows[0].perface) {
        filePath = path.join(__dirname, `../public/images/${result.rows[0].perface}`);
    }
    else {
        filePath = path.join(__dirname, `../public/images/${result.rows[0].address}`);
    }
    let fileContent = fs.readFileSync(filePath)
    ctx.set("Content-Type", "image/jpg")
    ctx.body = fileContent;
})


module.exports = router